Ext.namespace("GridInfo");

GridInfo.Detail=function(plist){
	this.parentList = plist;
	this.userSelector = null;
	this.selectUserId = null;
	this.users = [];
	
	//网格ID
	this.txtGridId = new Ext.form.Hidden({
		name:'gridId'
	});
	
	//网格类型
	this.cmbGridType = new Ext.form.ComboBox({
        width: 150,
        fieldLabel: '网格类型',
        name:'gridType',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GRID_NC_TYPE
        }),
        valueField: 'value',
        displayField: 'name',
        typeAhead:true,
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'',
        selectOnFocus: true
    });
	
    //网格名称
	this.txtGridName = new Ext.form.TextField({
		fieldLabel: '网格名称',
		allowBlank:false,
		blankText: '请输入网格名称!',
		name:'gridName',
		width:150
	});
	
	//网格状态
	this.cmbGridStatus = new Ext.form.ComboBox({
        width: 150,
        fieldLabel: '网格状态',
        name:'gridStatus',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GRID_NC_TYPE
        }),
        valueField: 'value',
        displayField: 'name',
        typeAhead:true,
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'',
        selectOnFocus: true
    });
	
    //网格经理
	this.txtGridManager = new Ext.form.TextField({
		fieldLabel: '网格经理',
		readOnly: true,
		width: 150,
		listeners: {
		   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt.ownerCt.ownerCt.ownerCt;
				cmp.getEl().on('click', self.showSelector, self); 
			}
		}
	});
	
	//最近营业厅
	this.cmbGridHill = new Ext.form.ComboBox({
        width: 150,
        fieldLabel: '最近营业厅',
        name:'hillId',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GRID_NC_TYPE
        }),
        valueField: 'value',
        displayField: 'name',
        typeAhead:true,
        mode: 'local',
        triggerAction: 'all',
        editable: false,
        emptyText:'',
        selectOnFocus: true
    });
    
    //生效日期
    this.dfEnableDate = new Ext.form.DateField({
    	fieldLabel :'生效日期:',
    	name : 'enableDate',
    	labelSeparator: '',
		width: 150,
		format: 'Y-m-d'
	});
	
	//失效日期
    this.dfDisableDate = new Ext.form.DateField({
    	fieldLabel :'失效日期:',
    	name : 'disableDate',
    	labelSeparator: '',
		width: 150,
		format: 'Y-m-d'
	});
    
   	//基本信息
	this.basicForm = new Ext.form.FormPanel({
		bodyStyle : 'padding: 5px 5px 0',
		frame :true,
		autoScroll : true,
		height: 390,
		width : 700,
		labelAlign: 'right',
		labelWidth : 120,
		layout : 'table',
		layoutConfig : {columns:2},
		items:[
			{colspan:2,layout:'form',items:[this.txtGridName,this.txtGridId]},
            {layout:'form', items:[this.cmbGridType]},
            {layout:'form', items:[this.cmbGridStatus]},
            {layout:'form', items:[this.txtGridManager]},
            {layout:'form',items:[this.cmbGridHill]}//,
            //{layout:'form',items:[this.dfEnableDate]},
            //{layout:'form',items:[this.dfDisableDate]}
		]
	});
	
	//GIS信息
	this.txtGisId = new Ext.form.Hidden({
		name: 'gisInfo.id'
	});
	
	this.txtGisType = new Ext.form.Hidden({
		name: 'gisInfo.type',
		value: 1
	});
	
	//所属区域
	this.cmbRegion = new Ext.form.ComboBox({
		width :150,
		listWidth:167,
		allowBlank : false,
		blankText:'请选择所属区域',
		fieldLabel: '所属区域',
		store : new Ext.data.JsonStore({
		   url:'../gridareas/listAllRegions.form',
		   autoLoad : true,
		   root:"regions",
		   fields:["areaId","name"]
		}),
		valueField : 'areaId',
		displayField :'name',
		editable: false,
        mode: 'local',
		triggerAction :'all',
		emptyText :'请选择所在区域',
		selectOnFocus :true	
	});
    
    //坐标
    this.txtCoordsInfo = new Ext.form.TextArea({
		fieldLabel : '网格坐标',
		preventScrollbars : true,
		name :'coordsInfo',
		width : 400,
		heigth : 350
    });
    
    this.txtMarketIdInfo = new Ext.form.Hidden({
    	name:'marketIdInfo'
    });
    
    //网格边框颜色
    this.txtLineColor = new Ext.form.ComboBox({
        width: 120,
        listWidth : 137,
        fieldLabel: '边框颜色',
        name : 'lineColor',
        mode: 'local',
        editable: false,
        emptyText:'请选择颜色',
        triggerAction: 'all',
        editable: false,
        selectOnFocus: true,
        valueField: 'value',
        displayField: 'name',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GIS_COLOR
        })
    });
    
    //网格填充颜色
    this.txtMainFillColor = new Ext.form.ComboBox({
        width: 120,
        listWidth : 137,
        fieldLabel: '填充颜色',
        name : 'mainFillColor',
        mode: 'local',
        emptyText:'请选择颜色',
        triggerAction: 'all',
        editable: false,
        selectOnFocus: true,
        editable: false,
        valueField: 'value',
        displayField: 'name',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GIS_COLOR
        })
    });
    
    /*//网格名字坐标
    this.txtNamePos = new Ext.form.Hidden({
    	name: 'namePos'
    });*/
    
    /*//网格矩形外包坐标
     this.txtRectScale = new Ext.form.Hidden({
    	name: 'rectScale'
    });*/
    //绘制网格
    this.btnDrawGrid = new Ext.Button({
        text: '绘制网格',
        tooltip: {text: '绘制网格'},
        handler: this.viewGis,
        scope: this
    });
    
    //GIS信息--FormPanel
    this.gisform = new Ext.form.FormPanel({
		bodyStyle : 'padding: 5px 5px 0',
		frame :true,
		height:420,
		width : 700,
		labelAlign: 'right',
		labelWidth : 120,
		layout : 'table',
		layoutConfig : {columns:2},
		items:[
			{colspan:2,layout:'form',items:[this.cmbRegion]},
			{layout:'form', items:[this.txtLineColor]},
            {layout:'form', items:[this.txtMainFillColor]},
			{colspan:2,layout:'form', items:[this.txtCoordsInfo]},
			{layout:'form', items:[this.txtGisType]},
            {colspan:2,layout:'form', items:[this.btnDrawGrid]},
            {layout:'form', items:[this.txtGisId]}
		]
	});
		
	 this.tabs = new Ext.TabPanel({
        width:700,
        activeTab: 0,
        frame:true,
        height : 420,
        deferredRender:false, 
		layoutOnTabChange:true,
		
        items:[
            {items:this.basicForm, title: '基本信息'},
            {items:this.gisform, title: 'Gis信息'}      
        ]
    });
    
	GridInfo.Detail.superclass.constructor.call(this,{
		title : '网格信息',
		bodyStyle: 'padding: 5px 5px 0',
        autoHeight: true,
        width: 730,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items :[this.tabs],
        buttons: [{
            text: '保存',
            cls: 'x-btn-text-icon btn-save',
            handler: this.onSave,
            scope: this
        }, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
		
	});
	/*this.connSave = new Ext.data.Connection({
		url: '../gridareas/saveGridInfo.form?timesnap=' + Date.parse(new Date()),
		method: 'POST'
	});*/
};

Ext.extend(GridInfo.Detail,Ext.Window,{
	initData : function(id) {
			this.basicForm.getForm().reset();
			this.gisform.getForm().reset();
			var connection = new Ext.data.Connection({
						url : '../gridareas/getGridInfoById.form?timesnap=' + Date.parse(new Date()),
						method : 'POST'
					});
			if(id!=null){
				Utils.request(connection, {
						params : {
							gridId : id
						},
						scope : this,
						success : function(resp, action) {
							var gridinfo = resp.gridInfo;
							var gis = resp.gisInfo;

							//网络ID
							this.scope.txtGridId.setValue(gridinfo.gridId);
							//网格类型
							this.scope.cmbGridType.setValue(gridinfo.gridType);
							//网格名称
							this.scope.txtGridName.setValue(gridinfo.gridName);
							//网格状态
							this.scope.cmbGridStatus.setValue(gridinfo.gridStatus);
							//网格经理
							if(gridinfo.gridManagerUser!=null){
								this.scope.txtGridManager.setValue(gridinfo.gridManagerUser.userName);
							}
							//最近营业厅
							this.scope.cmbGridHill.setValue(gridinfo.hillId);
							//所属区域
							this.scope.cmbRegion.setValue(gridinfo.areaId);
							
							//GisId
							this.scope.txtGisId.setValue(gis.id);						
							
							//坐标
							this.scope.txtCoordsInfo.setValue(gis.coordsInfo);
							//线色
							this.scope.txtLineColor.setValue(gis.lineColor);
							//网格填充颜色
							this.scope.txtMainFillColor.setValue(gis.mainFillColor);
							//网格经理ID
							this.selectUserId=gridinfo.gridManager;
						},
						after : function() {					
						}
					});
			}
		},
		onSave: function(btn, event) {
			var form =this.basicForm.getForm();
			var gisform = this.gisform.getForm();
			if(this.cmbRegion.getValue()==''){
				Utils.warn('请选择区域');
			}
			if (!form.isValid()||!gisform.isValid()){
				return;
			}
			form.doAction('submit', { 
				method : 'post',
				url : '../gridareas/saveGridInfo.form', 
				params: {
					'areaId' : this.cmbRegion.getValue(),
					'gridManager':this.selectUserId,
					'gisInfo.coordsInfo' : this.txtCoordsInfo.getValue(),
					'gisInfo.lineColor' : this.txtLineColor.getValue(),
					'gisInfo.mainFillColor' : this.txtMainFillColor.getValue(),
					'gisInfo.type' : this.txtGisType.getValue(),
					'marketIdInfo' : this.txtMarketIdInfo.getValue(),
					'gisInfo.id' : this.txtGisId.getValue()
				},
				scope: this,
				success: function(form, action) {
					//console.info(this);
					this.parentList.update();
					this.hide();
				},
				after: function() {}
			});
		},
		
		initGridGis:function(lineColor,mainFillColor,gridName,coordInfo,marketIdInfo){
			this.txtLineColor.setValue(lineColor);
			this.txtMainFillColor.setValue(mainFillColor);
			this.txtGridName.setValue(gridName);
			this.txtCoordsInfo.setValue(coordInfo);
			this.txtMarketIdInfo.setValue(marketIdInfo);//网格内营销单元ID集合
		},
		
		viewGis:function(){
			
			var areaId = this.cmbRegion.getValue();
			var gridId = this.txtGridId.getValue();
			if(areaId==''){
				Utils.warn('请先选择区域！');
				return;
			}
			if(gridId==null||gridId==""){
				gridId=0;
			}
			var str = OMS.GIS_IP+'/gis_for_management.jsp?areaId=' + areaId + 
			'&lineColor=' + this.txtLineColor.getValue() + 
			'&mainFillColor=' + this.txtMainFillColor.getValue() + 
			'&name='+this.txtGridName.getValue()+
			'&type='+this.txtGisType.getValue()+
			'&did='+ gridId + 
			'&coordx='+''+ '&layerType=1';
			window.open(encodeURI(str), '_blank', 'toolbar=no,location=no,directories=no,scrollbars=no,menubar=no,width=1000,height=600,resizable=no,left=0,top=0, status=no');	
		},
		showSelector: function(event) {
			if (!this.userSelector) {
				this.userSelector = new Selector.User(this, '网格经理');
			}
			//console.info(this.users);
			this.userSelector.initUsers(this.users);
			this.userSelector.show();
		},
		setUsers: function(o) {	// for Selector.User callback
			if(o.length>0){
				this.users = o;
				this.txtGridManager.setValue(this.users[0].name);
				this.selectUserId = this.users[0].id;
			}	
		}
});

//网格GIS 回调函数
 function initGridGis(lineColor,mainFillColor,gridName,coordInfo,marketIdInfo){
	subGrid.gridinfoPanel.initGridGis(lineColor,mainFillColor,gridName,coordInfo,marketIdInfo);
};

